package com.gym.code.generator.impl.dao.sql.impl;

import java.util.List;

import com.gym.code.generator.impl.dao.sql.WhereSqlGenerator;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.method.MethodSelectModel;
import com.gym.code.generator.utils.LocalStringUtils;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-15 下午8:21:45
 */
public class QuerySqlGenerator extends WhereSqlGenerator<MethodSelectModel> {

	private static QuerySqlGenerator instance = new QuerySqlGenerator();
	public static QuerySqlGenerator getInstance(){
		return instance;
	}
	private QuerySqlGenerator(){}
	
	@Override
	public String generate(MethodSelectModel methodModel) {
		
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("select ");
		sqlBuilder.append(generateQueryColumn(methodModel.getQueryResultList()));
		sqlBuilder.append(" from ");
		sqlBuilder.append(methodModel.getBeanGenerateModel().getTableName());
		sqlBuilder.append(" where 1 = 1 ");
		sqlBuilder.append(generateWhereColumn(methodModel.getParameterList()));
		return sqlBuilder.toString();
	}

	
	
	private String generateQueryColumn(List<FieldGenerateModel> queryResultList) {
		StringBuilder result = new StringBuilder();
		for(FieldGenerateModel item : queryResultList){
			result.append(item.getColumnName()).append(",");
		}
		return LocalStringUtils.removeLastChar(result.toString(),',');
	}
	
}



